//
//  NSString+Tool.m
//  GLGW_Shoping
//
//  Created by whx on 2018/12/6.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "NSString+Tool.h"

@implementation NSString (Tool)

+ (NSString *)stringWithTextFiledStr:(NSString *)textFlidStr replacementString:(NSString *)string
{
    NSString *endStr;
    if (string.length == 0) {
        NSMutableString *muStr = [NSMutableString stringWithFormat:@"%@",textFlidStr];
        if (textFlidStr.length >= 1) {
            NSString *newstr = [muStr substringToIndex:textFlidStr.length - 1];
            endStr = newstr;
        } else {
            endStr = muStr;
        }
    } else {
        endStr = textFlidStr;
    }
    return endStr;
}

+ (NSString *)randomStringWithLength:(NSInteger)len {
    NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
    for (NSInteger i = 0; i < len; i++) {
        [randomString appendFormat: @"%C", [letters characterAtIndex: arc4random_uniform([letters length])]];
    }
    return randomString;
}

/**
 给银行卡号4位一个kongge

 @param bankNum 银行卡号
 @return 添加好的银行卡号
 */
+(NSString *)getNewBankNumWitOldBankNum:(NSString *)bankNum
{
    if (bankNum.length) {
        NSString *text = [NSMutableString stringWithString:bankNum];
        NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
        text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSString *newString = @"";
        while (text.length > 0) {
            NSString *subString = [text substringToIndex:MIN(text.length, 4)];
            newString = [newString stringByAppendingString:subString];
            if (subString.length == 4) {
                newString = [newString stringByAppendingString:@" "];
            }
            text = [text substringFromIndex:MIN(text.length, 4)];
        }
        newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
        return newString;
    }
    return bankNum;
}

/**
 判断邮箱格式

 @param email 邮箱字符串
 @return 是否正确
 */
+ (BOOL)isValidateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailRegex];
    return [emailTest evaluateWithObject:email];
}

@end
